# 使用官方Python 3.11镜像作为基础镜像
FROM m.daocloud.io/docker.io/library/python:3.11-slim

# 设置工作目录
WORKDIR /app

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    DEBIAN_FRONTEND=noninteractive

    # 配置APT源
RUN apt-get update && \
    apt-get install -y apt-transport-https ca-certificates && \
    echo "deb https://mirrors.ustc.edu.cn/debian/ $(grep -oP 'VERSION_CODENAME=\K\w+' /etc/os-release) main contrib non-free" > /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/debian/ $(grep -oP 'VERSION_CODENAME=\K\w+' /etc/os-release)-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/debian-security/ $(grep -oP 'VERSION_CODENAME=\K\w+' /etc/os-release)-security main contrib non-free" >> /etc/apt/sources.list

RUN pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip3 config set global.trusted-host pypi.tuna.tsinghua.edu.cn
    
# 更新包管理器并安装系统依赖
RUN apt-get update && apt-get install -y \
    ffmpeg \
    wget \
    curl \
    && rm -rf /var/lib/apt/lists/*

# 验证FFmpeg安装
RUN ffmpeg -version

# 复制requirements.txt并安装Python依赖
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# 复制应用代码
COPY . .

# 创建必要的目录
RUN mkdir -p uploads output

# 设置目录权限
RUN chmod 755 uploads output

# 暴露端口
EXPOSE 8000

# 健康检查
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8000/ || exit 1

# 启动应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
